//
//  HOReportDetailViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOReportDetailViewController.h"
#import "HOUtils.h"
#import "HOEditView.h"
#import <AVFoundation/AVFoundation.h>
#import "HOUtils.h"
#import "HODefine.h"
#import "HOReportBaseInfoCell.h"
#import "HOReportDetailEntity.h"
#import "HOMaintainHistoryCell.h"
#import "HOReportHandlerCell.h"
#import "UIImageView+WebCache.h"
#import "HOMaintainHistoryEntity.h"
#import "HOConstants.h"
#import "HOReplyViewController.h"
#import "HOUploadImageView.h"
#import "HORecordView.h"
#import "HOCommentTableViewCell.h"
#import "HOReplyListViewController.h"
#import "HORepaireDoneViewController.h"
#import "HOConfirmOrderViewController.h"
#import "HOAssignViewController.h"
#import "HOReassignRepairerViewController.h"
#import "HOFaultCategoryViewController.h"
#import "HOVoiceDownloader.h"
#import "HOStaticDataMgr.h"
#import "HOAssignViewController.h"
#import "HOReportViewController.h"
#import "HOReportLocationViewController.h"
#import "HOEditInfoViewController.h"
#import "HODepartmentViewController.h"
#import "HOReportEditViewController.h"
#import "HOAssistantListViewController.h"
#import "JSHAREService.h"
#import "HOAddExtraAssistantViewController.h"

@interface HOReportDetailViewController()<HOAssignViewControllerDelegate, HOFaultCategoryViewControllerDelegate, HODepartmentViewControllerDelegate, HOReportLocationViewControllerDelegate>
{
	int myUid;
}

@property(nonatomic, retain)HOReportDetailEntity *detailEntity;
@property(nonatomic, retain)UIView		*menuPanel;
@property(nonatomic, assign)BOOL		canEdit;

@property(nonatomic, retain)HOCustomButtn *takeReportBtn;
@property(nonatomic, retain)HOCustomButtn *repairAgainBtn;
@property(nonatomic, retain)HODepartmentEntity *selectedDepartmentEntity;
@property(nonatomic, retain)UIImageView *urgentImgView;


@end

@implementation HOReportDetailViewController
@synthesize selectedDepartmentEntity;

- (void)viewDidLoad
{
	[super viewDidLoad];
//	[self initDataSource];
	[self initTableView];
	[self initSubtViews];
	[self initDataSource];
	[self registerTapGesture];
	[self initMediaViewNoRecord];
	self.menuPanel.hidden = YES;
	[self.recordView disableRecord];
	[self.uploadImgView hideUploadButton];
	
}

- (BOOL)isEnableCheckButton
{
	//是否返修按钮是否可以点击
	return NO;
}

- (void)initDataSource
{
	
	self.detailEntity = [[[HOReportDetailEntity alloc] init] autorelease];
	myUid = [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId];
	
}

- (void)requestData
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetOrderDetail,
								  kHO_FIELD_ReportType:[NSNumber numberWithInt:self.reportType],
										kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderEntity.orderId]}];
}

- (void)hideKeyboard
{
	
	self.menuPanel.hidden = YES;
}

- (void)addComment:(UIButton *)sender
{
	self.menuPanel.hidden = YES;
	HOReplyViewController *viewCtrl = [[HOReplyViewController alloc] init];
	viewCtrl.entity = self.orderEntity;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}
- (void)confirmComplete:(UIButton *)sender
{
	if(self.orderEntity.doneId == 0)
	{
		[self popupHintView:NSLocalizedString(@"此报修单还未维修完成，暂不能确认完成", @"此报修单还未维修完成，暂不能确认完成")];
		return;
	}
	HOConfirmOrderViewController *viewCtrl = [[HOConfirmOrderViewController alloc] init];
	viewCtrl.orderEntity = self.orderEntity;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)didSelectRepairer:(HORepairerEntity *)entity
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_ReAssignFault,
								  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderEntity.orderId],
								  kHO_FIELD_AssignId:[NSNumber numberWithInt:entity.userId],
								  kHO_FIELD_RecordStatus:[NSNumber numberWithInteger:1],
								  kHO_FIELD_Content:@""}];
}

- (void)didSelectFault:(HOFaultCategoryEntity *)entity withSubEntity:(HOFaultCategoryEntity *)subEntity;
{
	NSString *faultId = [NSString stringWithFormat:@"%d-%d", entity.faultId, subEntity.faultId];
	
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_UpdateFault,
								  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderEntity.orderId],
								  kHO_FIELD_DepartmentId:[NSNumber numberWithInt:self.selectedDepartmentEntity.departmentId],
								  kHO_FIELD_FaultId:faultId}];
}

- (void)repairDone:(UIButton *)sender
{
	HORepaireDoneViewController *viewCtrl = [[HORepaireDoneViewController alloc] init];
	viewCtrl.orderId = self.detailEntity.orderId;
	viewCtrl.isAssistantReport = self.detailEntity.isAssistantReport;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
	self.menuPanel.hidden = YES;
}

- (void)removeReport:(UIButton *)sender
{
	HOAlertView *amAlertView = [[HOAlertView alloc] initWithTitle:NSLocalizedString(@"您确认删除此报修吗?",  @"您确认删除此报修吗?")
														  message: nil
														 delegate: self
												cancelButtonTitle:NSLocalizedString(@"取消", @"取消")
												otherButtonTitles:NSLocalizedString(@"确定", @"确定"), nil];
	amAlertView.tag = [kHO_DIALOG_RemoveReport integerValue];
	[amAlertView show];
	[amAlertView release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(alertView.tag == [kHO_DIALOG_RemoveReport integerValue])
	{
		if( buttonIndex  == 1)
		{
			[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_DeleteFault,
										  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderEntity.orderId]}];
		}
	}
	else if(alertView.tag == [kHO_DIALOG_Confirm_TakeReport integerValue])
	{
		if( buttonIndex  == 1)
		{
			[self sendTakeRequest];
		}
	}
	else if(alertView.tag == [kHO_DIALOG_Confirm_RepairAgain integerValue])
	{
		if( buttonIndex  == 1)
		{
			[self repairAgainRequest];
		}
	}
	else
	{
		[super alertView:alertView clickedButtonAtIndex:buttonIndex];
	}
	
}

- (UIView *)createMenuItem
{
	CGFloat num = 0;
	UIView *view = [[[UIView alloc]
					initWithFrame:CGRectMake(self.view.frame.size.width - 130,
											 0,
											 130,
											 num * 35)] autorelease];
	[view setBackgroundColor:[UIColor whiteColor]];     
//	[view.layer setMasksToBounds:YES];
//	view.layer.borderWidth = [HOUtils defaultOnePixelConversion];
	if([[[HOStaticDataMgr sharedInstance] getUserInfo] isReportUser])
	{
		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"添加回复记录", @"添加回复记录") withSelector:@selector(addComment:) withY:0]];
		num ++;
//		NSLog(@"self.orderEntity.postId:%d", self.orderEntity.postId);
//		NSLog(@"[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId]:%d", [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId]);
		if(self.orderEntity.postId == [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId] && self.detailEntity.status == HO_REPORT_ORDER_REPAIRED)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"确认完成", @"确认完成") withSelector:@selector(confirmComplete:) withY:num * 35]];
			num ++;
		}
		
		if(self.orderEntity.postId == [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId])
		{
			if(self.detailEntity.status == HO_REPORT_ORDER_UNACCEPT)
			{
				
				[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"删除报修单", @"删除报修单") withSelector:@selector(removeReport:) withY:num * 35]];
				num ++;
			}
		}
		
	}
	else
	{
		if(self.detailEntity.status == HO_REPORT_ORDER_ACCEPTED || self.detailEntity.status == HO_REPORT_ORDER_UNACCEPT)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"分配人员", @"分配人员") withSelector:@selector(reassignTo:) withY:0]];
			num ++;
		}
		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"添加回复记录", @"添加回复记录") withSelector:@selector(addComment:) withY:num * 35]];
		num ++;
		if(self.detailEntity.doneId == [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId] && self.detailEntity.status == HO_REPORT_ORDER_ACCEPTED)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"维修完成", @"维修完成") withSelector:@selector(repairDone:) withY:num * 35]];
			num ++;
		}
		if(self.orderEntity.postId == [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId] && self.detailEntity.status == HO_REPORT_ORDER_UNACCEPT)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"删除报修单", @"删除报修单") withSelector:@selector(removeReport:) withY:num * 35]];
			num ++;
		}
		if(self.orderEntity.postId == [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId] && self.detailEntity.status == HO_REPORT_ORDER_REPAIRED)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"确认完成", @"确认完成") withSelector:@selector(confirmComplete:) withY:num * 35]];
			num ++;
		}
	}
	
	if(self.detailEntity.status != HO_REPORT_ORDER_REPAIRED
	   && self.detailEntity.status != HO_REPORT_ORDER_CONFIRMED
	   && self.orderEntity.postId == [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId])
	{
		//属于自己的报修单，报修单未完成的时候，报修人可以编辑报修单，编辑即重新进入报修页面
//		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"编辑报修单", @"编辑报修单") withSelector:@selector(editReport:) withY:num * 35]];
//		num ++;
	}
	//主任申请的，目前主任审核而又不需要院长审核，需要分享
	//需要分享
	[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"QQ分享", @"QQ分享") withSelector:@selector(shareReportByQQ:) withY:num * 35]];
	num ++;
	[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"微信分享", @"微信分享") withSelector:@selector(shareReportByWechat:) withY:num * 35]];
	num ++;
//
	[view setBackgroundColor:[UIColor whiteColor]];
	view.layer.shadowOffset = CGSizeMake(0.0, 0.0); //shadow on the right and bottom sides.
	view.layer.shadowRadius = 4.0;
	view.layer.shadowOpacity = 0.25;
	[self.view addSubview:view];
	view.hidden = YES;
	view.frame = CGRectMake(self.view.frame.size.width - 130,
				  0,
				  130,
				  num * 35);
	return view;
}

- (void)reassignTo:(UIButton *)sender
{
    //分配受理人
    if(![self canAssign])
    {
        return;
    }
    if(self.detailEntity.doneId == 0)
    {
        HOAssignViewController *viewCtrl = [[HOAssignViewController alloc] init];
        viewCtrl.delegate = self;
        viewCtrl.departmentId = self.detailEntity.faultDepartmentId;
        [self.navigationController pushViewController:viewCtrl animated:YES];
        [viewCtrl release];
    }
    else
    {
        HOReassignRepairerViewController *viewCtrl = [[HOReassignRepairerViewController alloc] init];
        viewCtrl.orderId = self.detailEntity.orderId;
        viewCtrl.departmentId = self.detailEntity.faultDepartmentId;
        viewCtrl.entity = self.detailEntity.repairerEntity;
        [self.navigationController pushViewController:viewCtrl animated:YES];
        [viewCtrl release];
    }
}

- (void)editReport:(id)sender
{
	HOReportEditViewController *reportViewCtrl = [[HOReportEditViewController alloc] init];
	reportViewCtrl.reportEntity = self.detailEntity;
	[self.navigationController pushViewController:reportViewCtrl animated:YES];
	
	[reportViewCtrl release];
	
	
}

- (UIButton *)createMenuItemWithTitle:(NSString *)title withSelector:(SEL)selector withY:(CGFloat)y
{
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
	btn.frame = CGRectMake(- 1,
						   y,
						   132,
						   35);
	
	[btn setTitle:title forState:UIControlStateNormal];
	btn.layer.borderColor = UIColorFromRGB(0xcdcdcd).CGColor;
	btn.layer.borderWidth = [HOUtils defaultOnePixelConversion];
	[btn setTitleColor:TEXT_COLOR_666666 forState:UIControlStateNormal];
	btn.titleLabel.font = [UIFont systemFontOfSize:14];
	[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
	return btn;
}

- (void)initSubtViews
{
	baseTableview.separatorStyle = UITableViewCellSelectionStyleNone;
	self.takeReportBtn = [[[HOCustomButtn alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80, self.view.frame.size.height - 150, 50, 50)] autorelease];
	[self.takeReportBtn setBackgroundImage:[UIImage imageNamed:@"btn_takereport"] forState:UIControlStateNormal];
	[self.view addSubview:self.takeReportBtn];
	[self.takeReportBtn addTarget:self action:@selector(takeReport:) forControlEvents:UIControlEventTouchUpInside];
	self.takeReportBtn.alpha = 0.8;
	
	self.repairAgainBtn = [[[HOCustomButtn alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80, self.view.frame.size.height - 150, 50, 50)] autorelease];
	[self.repairAgainBtn setBackgroundImage:[UIImage imageNamed:@"btn_repair_again"] forState:UIControlStateNormal];
	[self.view addSubview:self.repairAgainBtn];
	[self.repairAgainBtn addTarget:self action:@selector(repairAgain:) forControlEvents:UIControlEventTouchUpInside];
	self.repairAgainBtn.alpha = 0.8;
	
	[self updateStatus];
	
	
	self.urgentImgView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)] autorelease];
	//			imgView.layer.borderWidth = 1;
	[self.view addSubview:self.urgentImgView];
	self.urgentImgView.tag = 141;
	self.urgentImgView.image = [UIImage imageNamed:@"icon_urgent"];

}

- (void)updateStatus
{
	if((self.orderEntity.status == HO_REPORT_ORDER_CONFIRMED))
	{
		self.repairAgainBtn.hidden = NO;
	}
	else
	{
		self.repairAgainBtn.hidden = YES;
	}
	
	if(![self isMyRepairDepartment])
	{
		self.takeReportBtn.hidden = YES;
		return;
	}
	if( [[[HOStaticDataMgr sharedInstance] getUserInfo] isRepairUser]
	   && (self.orderEntity.status == HO_REPORT_ORDER_UNACCEPT))
	{
		self.takeReportBtn.hidden = NO;
	}
	else
	{
		self.takeReportBtn.hidden = YES;
	}
}


- (void)takeReport:(id)sender
{
	int departmentId = [[HOStaticDataMgr sharedInstance] getUserInfo].departmentId;
	if(departmentId != self.detailEntity.firstDepartmentEntity.departmentId)
	{
		HOAlertView *amAlertView = [[HOAlertView alloc] initWithTitle:NSLocalizedString(@"非您所在部门报修单，您确定要接单?",  @"非您所在部门报修单，您确定要接单?")
															  message: nil
															 delegate: self
													cancelButtonTitle:NSLocalizedString(@"关闭", @"关闭")
													otherButtonTitles:NSLocalizedString(@"确定", @"确定"), nil];
		amAlertView.tag = [kHO_DIALOG_Confirm_TakeReport integerValue];
		[amAlertView show];
		[amAlertView release];
	}
	else
	{
		[self sendTakeRequest];
	}
	
}



- (void)repairAgain:(id)sender
{
	HOAlertView *amAlertView = [[HOAlertView alloc] initWithTitle:NSLocalizedString(@"您确定要一键返修此报修单?",  @"您确定要一键返修此报修单?")
														 message: nil
														delegate: self
											   cancelButtonTitle:NSLocalizedString(@"关闭", @"关闭")
											   otherButtonTitles:NSLocalizedString(@"确定", @"确定"), nil];
	amAlertView.tag = [kHO_DIALOG_Confirm_RepairAgain integerValue];
	[amAlertView show];
	[amAlertView release];
}

- (void)sendTakeRequest
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_ReAssignFault,
								  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderEntity.orderId],
								  kHO_FIELD_AssignId:[NSNumber numberWithInt:[[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId]],
								  kHO_FIELD_RecordStatus:[NSNumber numberWithInteger:1],
								  kHO_FIELD_Content:@""}];
}

- (void)repairAgainRequest
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_AddNormalReport,
								  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderEntity.orderId],
								  kHO_FIELD_isReSubmit:@"1"}];
}


- (void)transferToExtra:(id)sender
{
	HOAddExtraAssistantViewController *viewCtrl = [[[HOAddExtraAssistantViewController alloc] init] autorelease];
	viewCtrl.companyId = [[HOStaticDataMgr sharedInstance] getCompanyId];
	viewCtrl.orderDetailEntity = self.detailEntity;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl requestOriginReport];
}

//- (void)hideKeyboard
//{
//	[[self.editView getTextView] resignFirstResponder];
//}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	self.menuPanel.hidden = YES;
}

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
	[super didReceiveCommand:action data:data];
	if([action isEqualToString:kHO_CMD_ReceiveNotificationMessage])
	{
		NSDictionary *dic = [data objectForKey:kHO_FIELD_Extras];
		int type = [[dic valueForKey:kHO_FIELD_SendType] intValue];
		int orderId = [[dic valueForKey:kHO_FIELD_OrderId] intValue];
		if(type == HO_NC_TYPE_ORDER_ASSIGNED && orderId == self.orderEntity.orderId)
		{
			[self requestData];
		}
	}
	if([action isEqualToString:kHO_CMD_Report_Detail_Changed])
	{
		[self requestData];
	}
}


#pragma mark --

- (void)sendNow:(HOEditView *)endView
{
	[self didClickFooterButton:nil];
}


- (void)viewWillAppear:(BOOL)animated
{
	self.uploadImgView.needDisplayRemoveButton = NO;
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"报修详情", "报修详情")];
	[self requestData];
	[baseTableview reloadData];
	[self updateStatus];
	
	self.urgentImgView.hidden = !self.orderEntity.isUrgent;
}

- (void)rightButtonItemDidClick
{
	self.menuPanel.hidden = !self.menuPanel.hidden;
}

- (void)dealloc
{
	self.detailEntity = nil;
	self.menuPanel = nil;
	self.takeReportBtn = nil;
	self.repairAgainBtn = nil;
	self.urgentImgView = nil;
	[super dealloc];
}

- (void)viewAllHistory:(id)sender
{
	
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
	{
		if(indexPath.row == 5)
		{
			UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(85,
																	  0,
																	  [HOUtils getScreenWidth] - 90,
																	  100)] autorelease];
			NSString *content = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"故障详情:", @"故障详情:"), self.detailEntity.detail];
			lbl.text = content;
			lbl.numberOfLines = 0;
			[lbl sizeToFit];
			return lbl.frame.size.height + HO_HORIZONAL_PADDING;
		}
		else if(indexPath.row == 3)
		{
			NSString *area = self.detailEntity.faultAreaName;
			UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(85,
																	 3,
																	 tableView.frame.size.width - 90,
																	 50)];
			lbl.numberOfLines = 2;
			lbl.text = area;
			[lbl sizeToFit];
			return 4 + MAX(lbl.frame.size.height, 22);
			
		}
		else
		{
			return 26;
		}
	}
	else if(indexPath.section == 1)
	{
		if(indexPath.row == 0)
		{
			if([self.detailEntity.imageList count] == 0)
				return 0;
			return self.singleImgWidth + HO_HORIZONAL_PADDING * 2;
		}
		else
		{
			return self.isRecordModal ?  50 + 128 + 22 : 50;
		}
	}
	else if(indexPath.section == 2)
	{
		return 28;
	}
	else if(indexPath.section == 3)
	{
		if(self.detailEntity.commentEntity == nil)
			return 110;
		HOCommentTableViewCell *cell = (HOCommentTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
		return [cell getCellHeight];
		
	}
	return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	if(self.detailEntity.commentEntity)
	{
		return 4;
	}
	else
	{
		return 4;
	}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section == 0)
		return 6;
	else if(section == 1)
		return 2;
	else if(section == 2)
		return 6;
	else if(section == 3)
	{
		
		return 1;
	}
	return 0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return 20.0;
	else if(section == 1 || section == 2)
		return SECTION_PADDING / 2;
	else if(section == 3)
		return 32;
	return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
	if(section == 3)
		return 32;
	return [HOUtils defaultOnePixelConversion];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
	CGFloat height = [self tableView:tableView heightForFooterInSection:section];
	UIView *view = [[[UIView alloc]
					initWithFrame:CGRectMake(0,
											 0,
											 tableView.frame.size.width,
											 height)] autorelease];
	if(section == 3 && self.detailEntity.commentEntity != nil)
	{
		UIButton *btn = [[UIButton alloc]
					  initWithFrame:CGRectMake(0,
											   0,
											   tableView.frame.size.width,
											   32)];
		[btn addTarget:self action:@selector(didSelectMore) forControlEvents:UIControlEventTouchUpInside];
		[btn setBackgroundImage:[HOUtils createImageWithColor:UIColorFromRGB(0xf5f5f5) forView:btn] forState:UIControlStateNormal];
		[btn setTitle:NSLocalizedString(@"查看更多>>", @"查看更多>>") forState:UIControlStateNormal];
		[btn setTitleColor:MAIN_COLOR_NORMAL forState:UIControlStateNormal];
		btn.titleLabel.font = [UIFont systemFontOfSize:13];
		[view addSubview:btn];
		[btn release];
	}
	return view;
}

- (void)didSelectMore
{
	HOReplyListViewController *viewCtrl = [[HOReplyListViewController alloc] init];
	viewCtrl.orderId = self.detailEntity.orderId;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
	UIView *view = [[[UIView alloc]
					initWithFrame:CGRectMake(0,
											 0,
											 tableView.frame.size.width,
											 height)] autorelease];
	if(section == 3)
	{
		UILabel *lbl = [[[UILabel alloc]
						initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
												 (height - 22) / 2,
												 tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
												 22)] autorelease];
		lbl.text = NSLocalizedString(@"报修单流转记录", @"报修单流转记录");
		lbl.font = [UIFont systemFontOfSize:13];
		lbl.textColor = UIColorFromRGB(0x666666);
		[view addSubview:lbl];
	}
	else if(section == 0 && self.orderEntity.isAssistantReport)
	{
		UIImageView *imgView = [[[UIImageView alloc] init] autorelease];
		imgView.frame = CGRectMake(0, 0, 50, 50);
		imgView.image = [UIImage imageNamed:@"icon_assistant"];
		[view addSubview:imgView];
	}
	return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
	{
		CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		static NSString *HOSearchTranslatorTableViewCellIdentifier = @"HOBaseInfoTableViewCellIdentifier";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOSearchTranslatorTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOSearchTranslatorTableViewCellIdentifier] autorelease];
		}
		
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryNone;
		
		UIView *upLineView = (UIView *)[cell viewWithTag:112];
		if(upLineView == nil)
		{
			
			upLineView = [[[UILabel alloc]
						 initWithFrame:CGRectMake(0,
												  0,
												  tableView.frame.size.width,
												  [HOUtils defaultOnePixelConversion])] autorelease];
			[upLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			upLineView.tag = 112;
			[cell.contentView addSubview:upLineView];
		}
		
		UIView *bottomLineView = (UIView *)[cell viewWithTag:113];
		if(bottomLineView == nil)
		{
			
			bottomLineView = [[[UILabel alloc]
						 initWithFrame:CGRectMake(0,
												  cellHeight - [HOUtils defaultOnePixelConversion],
												  tableView.frame.size.width,
												  [HOUtils defaultOnePixelConversion])] autorelease];
			[bottomLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			[cell.contentView addSubview:bottomLineView];
			bottomLineView.tag = 113;
		}
		bottomLineView.frame = CGRectMake(0,
										  cellHeight - [HOUtils defaultOnePixelConversion],
										  tableView.frame.size.width,
										  [HOUtils defaultOnePixelConversion]);
		upLineView.hidden = YES;
		bottomLineView.hidden = YES;
		UILabel *detailLbl = (UILabel *)[cell viewWithTag:114];
		if(detailLbl == nil)
		{
			detailLbl = [[[UILabel alloc]
						  initWithFrame:CGRectMake(85,
												   3,
												   tableView.frame.size.width - 90,
												   22)] autorelease];
			detailLbl.tag = 114;
			detailLbl.font = [UIFont systemFontOfSize:14];
			detailLbl.textColor = TEXT_COLOR_181818;
			[cell.contentView addSubview:detailLbl];
			cell.textLabel.font = [UIFont systemFontOfSize:14];
			cell.textLabel.textColor = TEXT_COLOR_181818;
		}
		
//		detailLbl.hidden = YES;
		detailLbl.numberOfLines = 1;
		UILabel *statusLbl = (UILabel *)[cell viewWithTag:1115];
		if(statusLbl == nil)
		{
			
			statusLbl = [[[UILabel alloc]
						  initWithFrame:CGRectMake(tableView.frame.size.width - 160,
												   3,
												   150,
												   22)] autorelease];
			statusLbl.font = [UIFont systemFontOfSize:15];
			statusLbl.tag = 1115;
			[cell.contentView addSubview:statusLbl];
		}
		statusLbl.hidden = YES;
		
		
		UILabel *lbl = (UILabel *)[cell viewWithTag:115];
		if(lbl)
		{
			lbl.hidden = YES;
		}
		if(indexPath.row == 0)
		{

			cell.textLabel.text = NSLocalizedString(@"报修单号:", @"报修单号:");
			detailLbl.text = [NSString stringWithFormat:@"%d", self.detailEntity.orderSN];
			upLineView.hidden = NO;
			UILabel *lbl = (UILabel *)[cell viewWithTag:115];
			if(lbl == nil)
			{
				
				lbl = [[[UILabel alloc]
								initWithFrame:CGRectMake(tableView.frame.size.width - 160,
														 3,
														 150,
														 22)] autorelease];
				lbl.textColor = TEXT_COLOR_999999;
				lbl.font = [UIFont systemFontOfSize:13];
				lbl.textAlignment = NSTextAlignmentRight;
				lbl.tag = 115;
				[cell.contentView addSubview:lbl];
			}
			lbl.hidden = NO;
			lbl.text = [HOUtils getDateFromTimeInterval:self.detailEntity.createTime * 1000];
			
		
		}
		else if(indexPath.row == 1)
		{
			cell.textLabel.text = NSLocalizedString(@"报修人科室:", @"报修人科室:");
			detailLbl.text = [HOUtils getValideString:self.detailEntity.postDepartmentName];
			detailLbl.frame = CGRectMake(100,
										 3,
										 cell.contentView.frame.size.width - 120,
										 22);
//			cell.selectionStyle = UITableViewCellSelectionStyleGray;
//			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			upLineView.hidden = NO;
		}
		else if(indexPath.row == 2)
		{
			cell.textLabel.text = NSLocalizedString(@"联系电话:", @"联系电话:");
			detailLbl.text = [HOUtils getValideString:self.detailEntity.phoneNumber];
//			upLineView.hidden = NO;
		}
		else if(indexPath.row == 3)
		{
			cell.textLabel.text = NSLocalizedString(@"报修地点:", @"报修地点:");
			detailLbl.text = [HOUtils getValideString:self.detailEntity.faultAreaName];
			detailLbl.numberOfLines = 2;
//			if(self.canEdit)
//			{
//				cell.selectionStyle = UITableViewCellSelectionStyleGray;
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			}
			detailLbl.frame = CGRectMake(85, 3,
					   cell.contentView.frame.size.width - 90,
										 50);
			[detailLbl sizeToFit];
			
			detailLbl.frame = CGRectMake(detailLbl.frame.origin.x,
										 (cellHeight - detailLbl.frame.size.height) / 2,
										 detailLbl.frame.size.width,
										 detailLbl.frame.size.height);
//			detailLbl.layer.borderWidth = 1;
		}
		else if(indexPath.row == 4)
		{
			cell.textLabel.text = NSLocalizedString(@"订单状态:", @"订单状态:");
			statusLbl.hidden = NO;
			if(self.detailEntity.status == 0)
				statusLbl.text = @"";
			else
			{
				statusLbl.text = [self.detailEntity getStatusStr];
				statusLbl.frame = CGRectMake(83,
											 3,
											 100,
											 22);
			}
			statusLbl.textColor = [HOUtils getOrderColorWithStatus:self.detailEntity.status];
		}
		else if(indexPath.row == 5)
		{
			bottomLineView.hidden = NO;
			cell.textLabel.text = NSLocalizedString(@"故障详情:", @"故障详情:");
			detailLbl.text = self.detailEntity.detail;
			detailLbl.numberOfLines = 0;
			detailLbl.frame = CGRectMake(85, 3,
										 cell.contentView.frame.size.width - 90,
										 50);
			[detailLbl sizeToFit];
			
			detailLbl.frame = CGRectMake(detailLbl.frame.origin.x,
										 (cellHeight - detailLbl.frame.size.height) / 2,
										 detailLbl.frame.size.width,
										 detailLbl.frame.size.height);
		}
		return cell;
	}
	else if(indexPath.section == 1)
	{
		CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		[self.uploadImgView setImagePaths:self.detailEntity.imageList];
		
		UITableViewCell *cell = [self tableView:tableView cellForOrderDetailRowAtIndexPath:indexPath];
		UIView *upLineView = (UIView *)[cell viewWithTag:112];
		if(upLineView == nil)
		{
			
			upLineView = [[[UILabel alloc]
						   initWithFrame:CGRectMake(0,
													0,
													tableView.frame.size.width,
													[HOUtils defaultOnePixelConversion])] autorelease];
			[upLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			upLineView.tag = 112;
			[cell.contentView addSubview:upLineView];
		}
		
		UIView *bottomLineView = (UIView *)[cell viewWithTag:113];
		if(bottomLineView == nil)
		{
			
			bottomLineView = [[[UILabel alloc]
							   initWithFrame:CGRectMake(0,
												  cellHeight - [HOUtils defaultOnePixelConversion],
												  tableView.frame.size.width,
												  [HOUtils defaultOnePixelConversion])] autorelease];
			[bottomLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			[cell.contentView addSubview:bottomLineView];
			bottomLineView.tag = 113;
		}
		bottomLineView.frame = CGRectMake(0,
										  cellHeight - [HOUtils defaultOnePixelConversion],
										  tableView.frame.size.width,
										  [HOUtils defaultOnePixelConversion]);
		if(indexPath.row == 0)
		{
			upLineView.hidden = NO;
			bottomLineView.hidden = YES;
		}
		else
		{
			upLineView.hidden = NO;
			bottomLineView.hidden = NO;
		}
	
		
		return cell;
	}
	else if(indexPath.section == 2)
	{
		CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		static NSString *HOSearchTranslatorTableViewCellIdentifier = @"HODetailTableViewCellIdentifier";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOSearchTranslatorTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOSearchTranslatorTableViewCellIdentifier] autorelease];
		}
		
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryNone;
		UIView *upLineView = (UIView *)[cell viewWithTag:112];
		if(upLineView == nil)
		{
			
			upLineView = [[[UILabel alloc]
						   initWithFrame:CGRectMake(0,
													0,
													tableView.frame.size.width,
													[HOUtils defaultOnePixelConversion])] autorelease];
			[upLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			upLineView.tag = 112;
			[cell.contentView addSubview:upLineView];
		}
		
		UIView *bottomLineView = (UIView *)[cell viewWithTag:113];
		if(bottomLineView == nil)
		{
			
			bottomLineView = [[[UILabel alloc]
							   initWithFrame:CGRectMake(0,
												  cellHeight - [HOUtils defaultOnePixelConversion],
												  tableView.frame.size.width,
												  [HOUtils defaultOnePixelConversion])] autorelease];
			[bottomLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			[cell.contentView addSubview:bottomLineView];
			bottomLineView.tag = 113;
		}
		bottomLineView.frame = CGRectMake(0,
										  cellHeight - [HOUtils defaultOnePixelConversion],
										  tableView.frame.size.width,
										  [HOUtils defaultOnePixelConversion]);
		upLineView.hidden = YES;
		bottomLineView.hidden = YES;
		
		
		UILabel *titleLbl = (UILabel *)[cell viewWithTag:114];
		if(titleLbl == nil)
		{
			
			titleLbl = [[[UILabel alloc]
						 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
												  3,
												  tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
												  22)] autorelease];
			titleLbl.textColor = TEXT_COLOR_181818;
			titleLbl.font = [UIFont systemFontOfSize:15];
			titleLbl.tag = 114;
			[cell.contentView addSubview:titleLbl];
		}
		if(indexPath.row == 0)
		{
			upLineView.hidden = NO;
			titleLbl.text = [NSString stringWithFormat:@"%@%0.2f%@", NSLocalizedString(@"修理金额:", @"修理金额:"), self.detailEntity.cost, @"元"];
		}
		else if(indexPath.row == 1)
		{
			titleLbl.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"希望受理人:", @"希望受理人:"), [HOUtils getValideString:self.detailEntity.hopeHandler]];
			
			cell.accessoryType = UITableViewCellAccessoryNone;
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
//			if(self.orderEntity.status > HO_REPORT_ORDER_ACCEPTED)
//			{
//				cell.accessoryType = UITableViewCellAccessoryNone;
//				cell.selectionStyle = UITableViewCellSelectionStyleNone;
//			}
//			else
//			{
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//				cell.selectionStyle = UITableViewCellSelectionStyleGray;
//			}
			
		}
		else if(indexPath.row == 2)
		{
			titleLbl.text = NSLocalizedString(@"协助维修:", @"协助维修:");
			cell.selectionStyle = UITableViewCellSelectionStyleGray;
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			
		}
		else if(indexPath.row == 3)
		{
			
			cell.selectionStyle = UITableViewCellSelectionStyleGray;
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			if(![[[HOStaticDataMgr sharedInstance] getUserInfo] isReportUser])
//			{
//				cell.selectionStyle = UITableViewCellSelectionStyleGray;
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			}

			titleLbl.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"受理人:", @"受理人:"), [HOUtils getValideString:self.detailEntity.handler]];
		}
		else if(indexPath.row == 4)
		{
//			int myUserId = [[HOStaticDataMgr sharedInstance] getUserInfo].userId;
			
			cell.selectionStyle = UITableViewCellSelectionStyleGray;
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			if((self.orderEntity.postId == myUserId
//			   || (self.detailEntity.doneId == myUserId && [[[HOStaticDataMgr sharedInstance] getUserInfo] isRepairUser]))
//			   && (self.orderEntity.status <= HO_REPORT_ORDER_ACCEPTED))
//			{
//				//如果单子还是未受理或者已受理下，报修人与当前维修人可以转单
//				cell.selectionStyle = UITableViewCellSelectionStyleGray;
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			}
//			else
//			{
//				cell.selectionStyle = UITableViewCellSelectionStyleGray;
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			}
			NSString *departmentName = self.detailEntity.postDepartmentName;
			titleLbl.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"负责部门:", @"负责部门"), [HOUtils getValideString:departmentName]];
		}
		else if(indexPath.row == 5)
		{
			bottomLineView.hidden = NO;
			
			cell.selectionStyle = UITableViewCellSelectionStyleGray;
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			if(![[[HOStaticDataMgr sharedInstance] getUserInfo] isReportUser])
//			{
//				cell.selectionStyle = UITableViewCellSelectionStyleGray;
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//			}
			titleLbl.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"故障分类:", @"故障分类:"), [HOUtils getValideString:self.detailEntity.category]];
		}
		return cell;
	}
	else if(indexPath.section == 3)
	{
		if(self.detailEntity.commentEntity == nil)
		{
			
			static NSString *HONODataTableViewCellIdentifier = @"HONODataTableViewCellIdentifier3";
			UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HONODataTableViewCellIdentifier];
			if(cell == nil)
			{
				cell = [[[UITableViewCell alloc]
						 initWithStyle:UITableViewCellStyleDefault
						 reuseIdentifier: HONODataTableViewCellIdentifier] autorelease];
				
			}
			[cell.contentView setBackgroundColor:UIColorFromRGB(0xf9f9f9)];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;

			UIImageView *imgView = (UIImageView *)[cell viewWithTag:142];
			if(imgView == nil)
			{
				UIImage *img = [UIImage imageNamed:@"baoxiuxiangqing_kongbaiye"];
				imgView = [[[UIImageView alloc]
							   initWithFrame:CGRectMake((tableView.frame.size.width - img.size.width) / 2,
														0,
														img.size.width,
														img.size.height)] autorelease];
				imgView.tag = 142;
				imgView.image = img;
				[cell.contentView addSubview:imgView];
			}
			imgView = (UIImageView *)[cell viewWithTag:142];
			UILabel *lbl = (UILabel *)[cell viewWithTag:143];
			if(lbl == nil)
			{
				lbl = [[[UILabel alloc]
							initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
														HO_HORIZONAL_PADDING + imgView.frame.size.height + HO_HORIZONAL_PADDING,
														tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
														22)] autorelease];
				lbl.tag = 143;
				[cell.contentView addSubview:lbl];
				lbl.textAlignment = NSTextAlignmentCenter;
				lbl.textColor = TEXT_COLOR_999999;
				lbl.font = [UIFont systemFontOfSize:13];
				lbl.text = NSLocalizedString(@"当前没有任何报修单流转记录", @"当前没有任何报修单流转记录");
			}
			return cell;
		}
		static NSString *HOCommentTableViewCellIdentifier = @"HOCommentTableViewCellIdentifier";
		HOCommentTableViewCell *cell = (HOCommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOCommentTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[HOCommentTableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOCommentTableViewCellIdentifier] autorelease];
			[cell initWithWidth:tableView.frame.size.width];
			
		}
		[cell setEntity:self.detailEntity.commentEntity];
		UIView *upLineView = (UIView *)[cell viewWithTag:112];
		if(upLineView == nil)
		{
			
			upLineView = [[[UILabel alloc]
						   initWithFrame:CGRectMake(0,
													0,
													tableView.frame.size.width,
													[HOUtils defaultOnePixelConversion])] autorelease];
			[upLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			upLineView.tag = 112;
			[cell.contentView addSubview:upLineView];
		}
		
		UIView *bottomLineView = (UIView *)[cell viewWithTag:113];
		if(bottomLineView == nil)
		{
			
			bottomLineView = [[[UILabel alloc]
							   initWithFrame:CGRectMake(0,
												  [cell getCellHeight] - [HOUtils defaultOnePixelConversion],
												  tableView.frame.size.width,
												  [HOUtils defaultOnePixelConversion])] autorelease];
			[bottomLineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
			[cell.contentView addSubview:bottomLineView];
			bottomLineView.tag = 113;
		}
		bottomLineView.frame = CGRectMake(0,
										  [cell getCellHeight] - [HOUtils defaultOnePixelConversion],
										  tableView.frame.size.width,
										  [HOUtils defaultOnePixelConversion]);
//		upLineView.hidden = YES;
//		bottomLineView.hidden = YES;
		
		
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryNone;
		return cell;
	}
	return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	self.menuPanel.hidden = YES;
	
	
	if(indexPath.section == 0 && indexPath.row == 1)
	{
		if(!self.canEdit)
			return;
		HODepartmentViewController *viewCtrl = [[HODepartmentViewController alloc] init];
		viewCtrl.delegate = self;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else if(indexPath.section == 0 && indexPath.row == 3)
	{
		if(!self.canEdit)
			return;
		HOReportLocationViewController *viewCtrl = [[HOReportLocationViewController alloc] init];
		viewCtrl.delegate = self;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	
	else if(indexPath.section == 0 && indexPath.row == 5)
	{
		if(!self.canEdit)
			return;
		
		HOEditInfoViewController *viewCtl = [[[HOEditInfoViewController alloc]initWithModule:HO_MODULE_TYPE_USER] autorelease];
		viewCtl.limitedCount = 15;
		viewCtl.navItemTitle = NSLocalizedString(@"更新故障详情", @"更新故障详情");
		viewCtl.fieldName = kHO_FIELD_NickName;
		viewCtl.placeHolder = NSLocalizedString(@"更新故障详情", @"更新故障详情");
		viewCtl.needHideLimitedLbl = YES;
		viewCtl.oldValue = self.detailEntity.detail;
	}
	
	else if(indexPath.section == 2 && indexPath.row == 2)
	{
		//协助维修
		if(![self canAssistant])
			return;
		HOAssistantListViewController *viewCtrl = [[HOAssistantListViewController alloc] init];
		viewCtrl.orderEntity = self.orderEntity;
		viewCtrl.departmentId = self.detailEntity.faultDepartmentId;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else if(indexPath.section == 2 && indexPath.row == 3)
	{
        [self reassignTo:nil];

	}
	else if(indexPath.section == 2 && indexPath.row == 4)
	{
		//修改负责部门
		if(![self canModifyDepartment])
			return;
		NSMutableArray *array = [NSMutableArray array];
		[array addObject:[NSNumber numberWithInt:self.detailEntity.firstDepartmentEntity.departmentId]];
		HODepartmentViewController *viewCtrl = [[HODepartmentViewController alloc] init];
		viewCtrl.exceptDepartmentIds = array;
		viewCtrl.delegate = self;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else if(indexPath.section == 2 && indexPath.row == 5)
	{
		//修改故障类型
		if(![self canModifyType])
			return;
		HOFaultCategoryViewController *viewCtrl = [[HOFaultCategoryViewController alloc] init];
		viewCtrl.delegate = self;
		viewCtrl.departmentId = self.detailEntity.faultDepartmentId;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
}

- (BOOL)canAssign
{
	if(self.orderEntity.status == HO_REPORT_ORDER_UNACCEPT)
	{
		if([self isMyRepairDepartment] && [[[HOStaticDataMgr sharedInstance] getUserInfo] isRepairUser])
		{
			//未受理，本部门，维修工,可以分配
			return YES;
		}
	}
	if(self.orderEntity.status == HO_REPORT_ORDER_ACCEPTED )
	{
		if([self isMyAccept])
		{
			//自己受理的单子
			return YES;
		}
	}
	
	[self popupHintView:@"您无权操作!"];
	return NO;
}

- (BOOL)canModifyDepartment
{
	if(self.orderEntity.status == HO_REPORT_ORDER_UNACCEPT)
	{
		if([self isMyReport])
		{
			//未受理，自己报的，可以修改负责部门
			return YES;
		}
	}
	if(self.orderEntity.status == HO_REPORT_ORDER_ACCEPTED )
	{
		if([self isMyRepairDepartment])
		{
			if( [self isMyAccept])
			{
				//本部门，自己受理，可以修改负责部门
				return YES;
			}
		}
	}
	
	[self popupHintView:@"您无权操作!"];
	return NO;
}


- (BOOL)canModifyType
{
	//修改故障类型
	return [self canModifyDepartment];
}

- (BOOL)canAssistant
{
	if([self isMyAccept] && !self.isFromAssistantList)
	{
		return YES;
	}
	
	[self popupHintView:@"您无权操作!"];
	return NO;
}

- (BOOL)isMyRepairDepartment
{
	//是否本部门
	if(![[[HOStaticDataMgr sharedInstance] getUserInfo] isRepairUser])
		return NO;
	int departmentId = [[HOStaticDataMgr sharedInstance] getUserInfo].departmentId;
	int  reportDepartmentId = self.detailEntity.firstDepartmentEntity.departmentId;
	return  departmentId == reportDepartmentId;
}

- (BOOL)isMyReport
{
	//是否我报的
	int myUserId = [[HOStaticDataMgr sharedInstance] getUserInfo].userId;
	return self.orderEntity.postId == myUserId;
}


- (BOOL)isMyAccept
{
	if(![[[HOStaticDataMgr sharedInstance] getUserInfo] isRepairUser])
		return NO;
	//是否我报的
	int myUserId = [[HOStaticDataMgr sharedInstance] getUserInfo].userId;
	return self.orderEntity.doneId == myUserId;
}

- (void)didSelectDepartment:(HODepartmentEntity *)entity
{
	self.isReturnFromDeptmentList = YES;
	self.selectedDepartmentEntity = entity;
	HOFaultCategoryViewController *viewCtrl = [[HOFaultCategoryViewController alloc] init];
	viewCtrl.delegate = self;
	viewCtrl.currentEntity = self.selectedFaultGategoryEntity;
	viewCtrl.subEntity = self.selectedSubFaultGategoryEntity;
	viewCtrl.departmentId = self.selectedDepartmentEntity.departmentId;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
	
}

- (void)gotoAssistant
{
	
}

- (void)shareReportByQQ:(UIButton *)sender
{
	[self shareReport:JSHAREPlatformQQ];
}

- (void)shareReportByWechat:(UIButton *)sender
{
	[self shareReport:JSHAREPlatformWechatSession];
}

- (void)shareReport:(JSHAREPlatform)platform
{
	JSHAREMessage *message = [JSHAREMessage message];
	message.mediaType = JSHARELink;
	message.url = [NSString stringWithFormat:@"http://www.baoxiuba.cn/normalorder.html?OrderId=%d&CompanyId=%@", self.orderEntity.orderId, [[HOStaticDataMgr sharedInstance] getUserInfo].companyId];
	message.text = self.orderEntity.detail;
	message.title = @"微度报修单详情";
	message.platform = platform;
	NSString *imageURL = @"http://www.baoxiuba.cn/Public/img/tyIcon.png";
	NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
	
	message.image = imageData;
	[JSHAREService share:message handler:^(JSHAREState state, NSError *error) {
		
		NSLog(@"分享回调");
		
	}];
}



				
#pragma mark

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_AddNormalReport])
	{
		[self cleanup];
//		[self popupHintView:@"紧急报修成功!"];
		self.view.userInteractionEnabled = NO;
		[self sendCommand:kHO_CMD_Report_List_Changed data:nil];
		[self popupHintView:@"已重新返修此单!" inTime:2 needCallback:YES];
		[baseTableview reloadData];
	}
	else if([response isEqualAction:kHO_ACTION_GetOrderDetail])
	{
		self.detailEntity.orderId = self.orderEntity.orderId;
		[self.detailEntity parseDic:response.dataNode];
		self.orderEntity.status = self.detailEntity.status;
		self.orderEntity.owerId = self.detailEntity.owerId;
		self.orderEntity.virtualCellPhone = self.detailEntity.virtualCellPhone;
		self.orderEntity.doneId = self.detailEntity.doneId;
		self.orderEntity.isUncompleted = self.detailEntity.isUncompleted;
		self.voiceFileURL = self.detailEntity.voiceURL;
		self.isNeedRerepair = self.detailEntity.isResubmit == 1;
		self.totalTime = self.detailEntity.totalTime;
		[self downloadVoice];
		
		self.menuPanel = [self createMenuItem];
		[self displayRightButtomItemWithTitle:NSLocalizedString(@"更多操作", @"更多操作")];
		
		[self updateStatus];
		[baseTableview reloadData];
	}
	else if([response isEqualAction:kHO_ACTION_ReAssignFault])
	{
		self.view.userInteractionEnabled = NO;
		[self popupHintView:@"接单或分配受理人成功!"];
		[self sendCommand:kHO_CMD_Report_List_Changed data:nil];
//		[self.navigationController popToRootViewControllerAnimated:NO];
//		[self sendCommand:kHO_CMD_TakeReport data:nil];
		[self requestData];
		[self updateStatus];
	}
	else if([response isEqualAction:kHO_ACTION_UpdateFault])
	{
		self.view.userInteractionEnabled = NO;
		[self popupHintView:@"转单或更换故障类型成功!"];
		[self sendCommand:kHO_CMD_Report_List_Changed data:nil];
		[self requestData];
		[self updateStatus];
	}
	else if([response isEqualAction:kHO_ACTION_DeleteFault])
	{
		self.view.userInteractionEnabled = NO;
		[self sendCommand:kHO_CMD_Report_List_Changed data:nil];
		[self popupHintView:NSLocalizedString(@"删除报修单成功!", @"删除报修单成功!")  inTime:1.5 needCallback:YES];
	}
	
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self.navigationController popViewControllerAnimated:YES];
}

@end
